package com.jzr.board.ui.fragment.todyworkfragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.jzr.board.R
import com.jzr.board.base.BaseEvenBus
import com.jzr.board.base.BaseFragment
import com.jzr.board.bean.BoardGetBean
import com.jzr.board.ui.adapter.todaywork.GeneralNurseLevelAdapter
import com.jzr.board.ui.adapter.todaywork.GeneralNurseServiceAdapter
import com.jzr.board.utils.PreferUtil
import kotlinx.android.synthetic.main.include_general_bed.*
import kotlinx.android.synthetic.main.include_general_nursing.*
import kotlinx.android.synthetic.main.include_general_statistics.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe

class GeneralViewFragment : BaseFragment() {

    private var data: BoardGetBean? = null

    private var adapter: GeneralNurseLevelAdapter? = null
    private var leftAdapter: GeneralNurseServiceAdapter? = null
    private var rightAdapter: GeneralNurseServiceAdapter? = null

    private var leftData = mutableListOf<BoardGetBean.DataBean.CareLabelSumsBean>()
    private var rightData = mutableListOf<BoardGetBean.DataBean.CareLabelSumsBean>()

    companion object {
        fun newInstance(data: BoardGetBean?): GeneralViewFragment? {
            val sf = GeneralViewFragment()
            sf.data = null
            sf.data = data

            return sf
        }

    }

    override fun getLayoutResId(): Int {
        return R.layout.fragment_generalview
    }

    override fun attachView() {

    }

    @Subscribe
    fun getEvenBusMessage(baseMessage: BaseEvenBus) {
        when (baseMessage.messageId) {
            BaseEvenBus.GETBOARDGET_T -> {
                initView(baseMessage.boardGetBean)
            }
        }
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }

        tvBed.setText("床位信息")
        tvNursing.setText("护理信息")
        tvStatistice.setText("统计信息")
        tvCall.setText("呼叫信息")

        initView(data)

    }

    fun  initView(data: BoardGetBean? ){
        if (data != null) {
            leftData.clear()
            rightData.clear()
            tvTotalInPat.text = data.data?.totalInPat.toString()
            tvTodayIn.text = data.data?.todayIn.toString()
            tvNextDayOut.text = data.data?.todayOut.toString()
            adapter = GeneralNurseLevelAdapter(data.data?.nurseLevelSums)
            rvNurseLevel.adapter = adapter
            val manage = LinearLayoutManager(activity)
            manage.orientation = LinearLayoutManager.HORIZONTAL
            rvNurseLevel.layoutManager = manage

            if(data.data?.careLabelSums?.size!=null){
                val postion = data.data?.careLabelSums?.size!! / 2
                for ((index, item) in data.data?.careLabelSums!!.withIndex()) {
                    if (index < postion) {
                        leftData.add(item)
                    }
                }

                for (i in postion until data.data?.careLabelSums!!.size) {
                    rightData.add(data.data?.careLabelSums!![i])
                }

                leftAdapter = GeneralNurseServiceAdapter(leftData)
                rightAdapter = GeneralNurseServiceAdapter(rightData)
                rvLeft.adapter = leftAdapter
                rvRight.adapter = rightAdapter

                val leftManage = LinearLayoutManager(activity)
                leftManage.orientation = LinearLayoutManager.VERTICAL
                rvLeft.layoutManager = leftManage

                val rightManage = LinearLayoutManager(activity)
                rightManage.orientation = LinearLayoutManager.VERTICAL
                rvRight.layoutManager = rightManage

            }
        }
    }

}